package com.ihr360.payroll.service.payroll.convertor;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.config.SalaryTaxEntity;
import com.ihr360.payroll.config.SalaryTaxRuleEntity;
import com.ihr360.payroll.model.dto.salary.tax.SalaryTax;
import com.ihr360.payroll.model.dto.salary.tax.SalaryTaxRule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SalaryTaxConvertor implements IConvertor<SalaryTaxEntity, SalaryTax> {

    @Autowired
    private SalaryTaxRuleConvertor salaryTaxRuleConvertor;

    @Override
    public SalaryTax convertEntityToDto(SalaryTaxEntity entity) {
        return null;
    }

    @Override
    public SalaryTaxEntity convertDtoToEntity(SalaryTax dto) {
        if (dto == null) {
            return null;
        }
        SalaryTaxEntity salaryTaxEntity = new SalaryTaxEntity();
        BeanUtils.copyProperties(dto, salaryTaxEntity);
        List<SalaryTaxRule> rules = dto.getRules();
        if (CollectionUtils.isNotEmpty(rules)) {
            List<SalaryTaxRuleEntity> ruleEntities = salaryTaxRuleConvertor.convertDtosToEntities(rules);
            salaryTaxEntity.setRules(ruleEntities);
        }
        return salaryTaxEntity;
    }

    public List<SalaryTaxEntity> convertDtosToEntities(List<SalaryTax> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return Lists.newArrayList();
        }
        List<SalaryTaxEntity> entities = Lists.newArrayListWithCapacity(dtos.size());
        dtos.forEach(dto -> {
            entities.add(convertDtoToEntity(dto));
        });
        return entities;
    }

}
